package com.example.weather_app.core;

import android.graphics.LightingColorFilter;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class BrightnessUtils {

    private static final String TAG = "BrightnessUtils";
    private static final String TIMEZONE = "GMT+8"; // 中国标准时区

    /**
     * 根据日出日落时间判断是否为夜间
     * @param sunrise 日出时间，格式为"HH:mm"
     * @param sunset 日落时间，格式为"HH:mm"
     * @return 是否为夜间
     */
    public static boolean isNightTime(String sunrise, String sunset) {
        if (sunrise == null || sunset == null) {
            Log.d(TAG, "sunrise or sunset is null, return false");
            return false;
        }

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
        sdf.setTimeZone(TimeZone.getTimeZone(TIMEZONE)); // 显式设置时区

        try {
            Date now = new Date();
            Log.d(TAG, "Current time: " + sdf.format(now));

            Date riseTime = sdf.parse(sunrise);
            Log.d(TAG, "Sunrise time parsed: " + sdf.format(riseTime));

            Date setTime = sdf.parse(sunset);
            Log.d(TAG, "Sunset time parsed: " + sdf.format(setTime));

            // 构建今天的日期（00:00:00）
            Calendar cal = Calendar.getInstance();
            cal.setTime(now);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            Date today = cal.getTime();
            Log.d(TAG, "Today start: " + sdf.format(today));

            // 计算今日的日出和日落时间（考虑日期）
            long riseMillis = riseTime.getTime() - sdf.parse("00:00").getTime();
            long setMillis = setTime.getTime() - sdf.parse("00:00").getTime();
            Date todayRise = new Date(today.getTime() + riseMillis);
            Date todaySet = new Date(today.getTime() + setMillis);

            Log.d(TAG, "Today sunrise: " + sdf.format(todayRise));
            Log.d(TAG, "Today sunset before adjustment: " + sdf.format(todaySet));

            // 处理跨天情况（日落时间在次日）
            if (todaySet.before(todayRise)) {
                todaySet = new Date(todaySet.getTime() + 24 * 60 * 60 * 1000);
                Log.d(TAG, "Sunset is before sunrise, adjusted today sunset: " + sdf.format(todaySet));
            }

            boolean isNight = now.before(todayRise) || now.after(todaySet);
            Log.d(TAG, "Is night time: " + isNight);
            Log.d(TAG, "Now: " + sdf.format(now) +
                    ", Today sunrise: " + sdf.format(todayRise) +
                    ", Today sunset: " + sdf.format(todaySet));

            return isNight;
        } catch (ParseException e) {
            Log.e(TAG, "Error parsing time", e);
            return false;
        }
    }

    /**
     * 调整视图背景亮度
     * @param view 要调整的视图
     * @param isNight 是否为夜间模式
     */
    public static void adjustBackgroundBrightness(View view, boolean isNight) {
        Drawable bg = view.getBackground();
        if (bg == null) {
            Log.d(TAG, "Background is null, can't adjust brightness");
            return;
        }

        Log.d(TAG, "Adjusting background brightness, isNight: " + isNight);

        if (isNight) {
            // 夜间模式：降低亮度
            bg.setColorFilter(new LightingColorFilter(
                    0x7F7F7F, // 亮度因子，0x7F为50%亮度
                    0x000000  // 不叠加颜色
            ));
            Log.d(TAG, "Set night time brightness filter");
        } else {
            // 白天模式：清除滤镜
            bg.clearColorFilter();
            Log.d(TAG, "Cleared color filter for day time");
        }
        view.invalidate(); // 刷新视图
        Log.d(TAG, "View invalidated to apply brightness change");
    }
}